package com.bee.user.controller.web;

import com.bee.commons.utils.StringUtil;
import com.bee.commons.vo.Result;
import com.bee.commons.web.constrants.WebConst;
import com.bee.user.common.domain.RoleDO;
import com.bee.user.common.vo.RoleVO;
import com.bee.user.service.RoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 角色表(Role)表控制层
 *
 * @author makejava
 * @since 2019-08-27 10:19:14
 */
@Slf4j
@Api(tags = "角色管理模块")
@RestController
@RequestMapping(WebConst.MANAGEMENT + "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "分页查询角色数据")
    @GetMapping("/getList")
    public Result<PageInfo<RoleVO>> getList(
            @ApiParam(value = "角色名称") @RequestParam(required = false) String roleName,
            @ApiParam(value = "状态") @RequestParam(required = false) Boolean enable,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") int pageNo,
            @ApiParam(value = "条数", defaultValue = "20") @RequestParam(defaultValue = "20") int pageSize
    ) {
        RoleVO roleVO = new RoleVO();
        if (StringUtil.isNotEmpty(roleName)) {
            roleVO.setName(roleName);
        }
        if (enable != null) {
            roleVO.setEnable(enable);
        }
        PageInfo<RoleVO> pageInfo = roleService.queryPage(roleVO, pageNo, pageSize);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "查询单条角色数据")
    @GetMapping()
    public Result<RoleDO> get(
            @ApiParam(value = "id主键") @RequestParam Long id
    ) {
        RoleDO roleDO = roleService.queryById(id);
        return Result.success(roleDO);
    }

    @ApiOperation(value = "添加角色")
    @PostMapping()
    public Result<?> add(
            @ApiParam(value = "角色名称") @RequestParam String name,
            @ApiParam(value = "角色名称") @RequestParam(required = false) Long orgId
    ) {
        RoleDO roleDO = new RoleDO();
        roleDO.setName(name);
        roleDO.setOrgId(orgId);
        roleService.insert(roleDO);
        return Result.success(null);
    }

    @ApiOperation(value = "修改角色")
    @PutMapping()
    public Result<?> update(RoleDO roleDO) {
        roleService.update(roleDO);
        return Result.success(null);
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping()
    public Result<?> delete(Long id) {
        if (roleService.deleteById(id)) {
            return Result.success(null);
        }
        return Result.fail("删除失败");
    }
}